import * as THREE from 'three'
import ThreeBSP from '../js/threeBSP'
import {
    scene,
    camera,
    width,
    height,
    renderer,
    controls
} from '../scene'

let render = () => {
    controls.update()
    camera.updateProjectionMatrix()
    renderer.render(scene, camera)
}

renderer.setAnimationLoop(render)

const BSP = ThreeBSP(THREE)



const geometry = new THREE.BoxGeometry(10, 10, 10);
// 创建第一个方块
const cube = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0x00ff00 }));
scene.add(cube);
// 创建第二个方块
const cube1 = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({ color: 0xffff00 }));
scene.add(cube1);
cube1.position.setY(5)

// 基于创建的两个方块生成bsp实例
const sphereBSP = new BSP(cube)
const cubeBSP = new BSP(cube1)

// 交集
const resultBSPIntersect = sphereBSP.intersect(cubeBSP)

const intersect = resultBSPIntersect.toMesh()

const material = new THREE.MeshPhongMaterial({ color: 0xff0000 })

intersect.material = material
intersect.position.z = 20

scene.add(intersect)

// 合成
const resultBSPUnion = sphereBSP.union(cubeBSP)

const union = resultBSPUnion.toMesh()

union.material = material

union.position.x = -20

scene.add(union)

// 相减
const resultBSPSubtract = sphereBSP.subtract(cubeBSP)

const subtract = resultBSPSubtract.toMesh()

subtract.material = material
subtract.position.z = -20

scene.add(subtract)

